import pydelicious_

class Cloud:

    def __init__(self):
        self.counters = {}
        
    def add(self, key):
        c = self.counters.get(key,0) + 1
        self.counters[key] = c

    def freeze(self):
        deco = [(-v,k) for k,v in self.counters.iteritems()]
        deco.sort()
        self.keys = [k for _,k in deco]
        self.best = ""
        if self.keys != []:
            self.best = self.keys[0]

class UrlPost:

    def __init__(self, urlpost):
        self.user = urlpost['user']
        self.tags = urlpost['tags'].split() if urlpost['tags'] else []
        self.url = urlpost['href']
        self.title = urlpost['description']
        self.description = urlpost['extended']

class UserWithTags:

    def __init__(self, user, tags):
        self.user = user
        self.tags = tags
        self.nTags = len(tags)
        
    def __cmp__(self, other):
        return cmp(self.nTags, other.nTags)
        
class Url:
    def __init__(self, url):
        self.url = url
        self.tagCloud = Cloud()
        self.titleCloud = Cloud()
        self.descriptionCloud = Cloud()
        self.users = []

    def add(self, urlPost):
            for tag in urlPost.tags:
                self.tagCloud.add(tag)
            self.titleCloud.add(urlPost.title)
            if urlPost.description != "":
                self.descriptionCloud.add(urlPost.description)
            self.users.append(UserWithTags(urlPost.user, urlPost.tags))

    def freeze(self):
        self.tagCloud.freeze()
        self.titleCloud.freeze()
        self.descriptionCloud.freeze()
        self.users.sort()
        self.nUsers = len(self.users)

    def __cmp__(self,other):
        return cmp(-self.nUsers, -other.nUsers)
    
class QueryUrl(Url):

    def __init__(self, url):
        Url.__init__(self, url)
        
    def fetchSibUrls(self):
        self.sibUrlMap = {}
        self._fetchUrlPosts()
        self._fetchUrls()
        
    def _fetchUrlPosts(self):
        for one in pydelicious_.get_urlposts(self.url):
            self.add(UrlPost(one))

    def _fetchUrls(self):
        for u in self.users:
            self._fetchUrlsForUser(u)


    def _fetchUrlsForUser(self, u):
        tags = u.tags
        rss = pydelicious_.getrss(user=u.user,tag=' '.join(tags))
        while len(rss) <= 1 and tags != []:
            tags = tags[:-1]
            rss = pydelicious_.getrss(user=u.user,tag=' '.join(tags))
        relevance = len(tags)
        for one in rss:
            urlPost = UrlPost(one)
            self.sibUrlMap.setdefault(urlPost.url, SibUrl(urlPost.url, relevance))
            self.sibUrlMap[urlPost.url].add(urlPost)
                
    def freeze(self):
        Url.freeze(self)
        for sibUrl in self.sibUrlMap.itervalues():
            sibUrl.freeze()
        self.sibUrls = self.sibUrlMap.values()
        self.sibUrls.sort()
        
class SibUrl(Url):

    def __init__(self, url, relevance):
        Url.__init__(self, url)
        self.relevance = relevance

    def __cmp__(self,other):
        c1 = cmp(-self.nUsers, -other.nUsers)
        if c1 == 0:
            return cmp(-self.relevance, -other.relevance)
        else:
            return c1
